package com.atguigu.democustomer.controller;

import com.atguigu.democustomer.bean.Customer;
import com.atguigu.democustomer.service.CustomerService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@RestController
//@Controller+@ResponseBody
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    @RequestMapping(value = "customerByName")
    public String getCustomerByName(@RequestParam("name") String name){
        System.out.println("Customer:"+name);
        return "success";
    }

    @RequestMapping("customerById/{id}")
    public String getCustomerById(@PathVariable String id){

//        Customer customer = customerService.getById(id);
//        Customer customer = this.customerService.getById(id);


        QueryWrapper<Customer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        Customer customer = customerService.getOne(queryWrapper);
        return customer.toString();
    }

    @PostMapping("customer")
    public void saveCustomer(@RequestBody Customer customer){
        customerService.saveOrUpdate(customer);
    }
}
